package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.MyRecordDto;
import com.yami.shop.bean.app.dto.WxrecordDto;
import com.yami.shop.bean.model.WxRecord;
import com.yami.shop.bean.param.WxRecordParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mp.utils.JsonUtils;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.IWxRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/p/record")
@Api(tags = "记录接口")
@AllArgsConstructor
@Slf4j
public class RecordController {

    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private IWxRecordService iWxRecordService;

    /**
     * 选择订单配送地址
     */
    @GetMapping("/list")
    @ApiOperation(value = "用户地址列表", notes = "获取用户的所有地址信息")
    public ResponseEntity<List<WxrecordDto>> dvyList() {
        String userId = SecurityUtils.getUser().getUserId();
        List<WxRecord> wxRecords = iWxRecordService.list(new LambdaQueryWrapper<WxRecord>().eq(WxRecord::getUserId, userId));
        return ResponseEntity.ok(mapperFacade.mapAsList(wxRecords, WxrecordDto.class));
    }

    @PostMapping("/addRecord")
    @ApiOperation(value = "新增记录", notes = "新增记录")
    public ResponseEntity<String> addRecord(@Valid @RequestBody WxRecordParam addrParam) {
        String  userId= SecurityUtils.getUser().getUserId();
        WxRecord wxRecord = mapperFacade.map(addrParam, WxRecord.class);
        wxRecord.setUserId(userId);
        wxRecord.setCreateTime(new Date());
        wxRecord.setUpdateTime(new Date());
        iWxRecordService.save(wxRecord);
        log.debug("保存表单后我获取到的记录id-----------》{}", wxRecord.getId());
        return ResponseEntity.ok(wxRecord.getId());
    }

    /**
     * 订单列表接口
     */
    @GetMapping("/myRecord")
    public ResponseEntity<IPage<MyRecordDto>> myRecord(PageParam<MyRecordDto> page) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<MyRecordDto> myOrderDtoIpage = iWxRecordService.pageMyByUserId(page, userId);
        return ResponseEntity.ok(myOrderDtoIpage);
    }

}
